﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ChineseChess.Game.Rules
{
    class XiangRule : Rule
    {
        public override bool IsDown { get; set; }

        private List<Piece> pies = null;

        public override List<Point> getNextPos(Piece pie, List<Piece> pies)
        {
            this.pies = pies;
            List<Point> pList = new List<Point>();
            int x = pie.X + 2;
            int y = pie.Y + 2;
            this.Add(pList, pie, x, y);

            x = pie.X - 2;
            y = pie.Y + 2;
            this.Add(pList, pie, x, y);

            x = pie.X + 2;
            y = pie.Y - 2;
            this.Add(pList, pie, x, y);

            x = pie.X - 2;
            y = pie.Y - 2;
            this.Add(pList, pie, x, y);
            return pList;
        }

        private void Add(List<Point> pList, Piece pie, int x, int y)
        { 
            if (IsRight(pie, x, y))
            {
                pList.Add(new Point(x, y));
            }
        }

        private bool IsRight(Piece pie, int x, int y)
        {
            if (x >= 9 || x < 0 || y < 0 || y >= 10)
            {
                return false;
            }

            int RiverIndex = 0;
            if (IsDown)
            {
                RiverIndex = 5;
            }
            else
            {
                RiverIndex = 5;
            }

            if (IsDown && y < RiverIndex)
            {
                return false;
            }
            else if (IsDown == false && y > RiverIndex)
            {
                return false;
            }
            int x1 = pie.X > x ? x + 1 : x - 1;
            int y1 = pie.Y > y ? y + 1 : y - 1;
            if (Chessboard.getPieByXy(x1, y1,pies) != null)//判断象心点有没有棋
            {
                return false;
            }

            Piece p = Chessboard.getPieByXy(x, y, pies);
            if (p != null && p.isRed == pie.isRed)//判断要走的坐标是否有自己一方的棋
            {
                return false;
            }
            return true;
        }
    }
}
